package com.pay.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.net.ssl.SSLSocketFactory;
import com.sun.net.ssl.HttpsURLConnection;


public class ServerPost {
	static public String post(String ip_postData,String ip_pageUrl){
		String strResult="";

			try {
				URL url=new URL(ip_pageUrl);
				
				URLConnection con=url.openConnection();
				if(con instanceof HttpsURLConnection){
					((HttpsURLConnection) con).setSSLSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault());
				}
				con.setDoOutput(true);
				con.setDoInput(true);
				//设置request headers的content类型和长度
				con.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
				con.setRequestProperty("Content-length", String.valueOf(ip_postData.length()));
				//发出post请求
				OutputStream outStream=con.getOutputStream();
				outStream.write(ip_postData.getBytes());
				outStream.flush();
				//读取应答请求
				InputStream inStream=con.getInputStream();
				while(true){
					int c=inStream.read();
					if(c==-1)
					  break;
					strResult=strResult+String.valueOf((char)c);
				}
				inStream.close();
				outStream.close();
				return strResult;
				
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		return null;
	}

}
